package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.dto.CommentInsertDto;
import com.woniu.car.dto.CommentQueryDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.entity.TServerDetail;
import com.woniu.car.entity.TServerOrder;
import com.woniu.car.entity.TServiceComment;

import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.mapper.TServiceCommentMapper;
import com.woniu.car.param.*;
import com.woniu.car.service.TServerDetailService;
import com.woniu.car.service.TServerOrderService;
import com.woniu.car.service.TServiceCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.woniu.car.dto.constant.CommonConstant.COMMENTUPDATENULL;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author *********
 * @since 2021-06-21
 */
@Service
public class TServiceCommentServiceImpl extends ServiceImpl<TServiceCommentMapper, TServiceComment> implements TServiceCommentService {

    @Resource
    private TServerOrderService tServerOrderService;
    @Resource
    private TServerDetailService tServerDetailService;

    /**
    *@Author 李磊
    *@Description 新增评论
    *@Param [commentInsertParam]
    *@return com.woniu.car.dto.CommentInsertDto
    **/
    @Override
    @Transactional
    public CommentInsertDto insert(CommentInsertParam commentInsertParam,Integer id){

        Integer detailId = commentInsertParam.getDetailId();

        TServerDetail byId2 = tServerDetailService.getById(detailId);
        if (null == byId2) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        Integer orderId = byId2.getOrderId();
        TServerOrder byId1 = tServerOrderService.getById(orderId);

        if (!byId1.getStatus().equals(CommonConstant.ONE)) {
            throw new ParamException(CommonConstant.COMMENTWRONG);
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id",orderId);
        List list = this.list(wrapper);

        if (null == list || list.size() > 0) {
            throw new ParamException(CommonConstant.COMMENTINSERTWRONG);
        }

        TServiceComment comment = new TServiceComment();
        BeanUtils.copyProperties(commentInsertParam,comment);
        comment.setOrderId(detailId);
        comment.setOrderNo(byId1.getOrderNumber());
        comment.setShopId(byId2.getShopId());
        comment.setUserId(id);
        comment.setOverTime(byId1.getCreateTime());

        this.save(comment);
        TServiceComment byId = this.getById(comment.getCommentId());
        CommentInsertDto commentInsertDto = new CommentInsertDto();

        BeanUtils.copyProperties(comment,commentInsertDto);

        return commentInsertDto;
    }

    /**
    *@Author 李磊
    *@Description 查询评论
    *@Param [commentQueryParam]
    *@return java.util.List<com.woniu.car.dto.CommentQueryDto>
    **/
    @Override
    @Transactional
    public List<CommentQueryDto> queryComment(CommentQueryParam commentQueryParam) throws Exception{

        if (null == commentQueryParam.getShopId() &&
                null == commentQueryParam.getOrderId() &&
                null == commentQueryParam.getUserId()) {
            throw new ParamException(CommonConstant.COMMENTQUERYWRONG);
        }

        QueryWrapper wrapper = new QueryWrapper();

        if (null != commentQueryParam.getShopId()) {
            wrapper.eq("shop_id",commentQueryParam.getShopId());
        }
        if (null != commentQueryParam.getUserId()) {
            wrapper.eq("user_id",commentQueryParam.getUserId());
        }
        if (null != commentQueryParam.getOrderId()) {
            wrapper.eq("order_id",commentQueryParam.getOrderId());
        }
        wrapper.eq("status",CommonConstant.ZERO);


        List<CommentQueryDto> list = this.list(wrapper);

        if (null == list || list.size() == 0) throw new ServiceSException(CommonConstant.RESULTNULL);

        return list;
    }

    /**
    *@Author 李磊
    *@Description 用户追评
    *@Param [upadateCommentByUserParam]
    *@return com.woniu.car.dto.CommentQueryDto
    **/
    @Override
    @Transactional
    public CommentQueryDto updateCommentByUser(UpadateCommentByUserParam upadateCommentByUserParam){

        Integer commentId = upadateCommentByUserParam.getCommentId();
        TServiceComment byId1 = this.getById(commentId);

        if (null == byId1) {
            throw new ParamException(COMMENTUPDATENULL);
        }

        TServiceComment comment = new TServiceComment();
        BeanUtils.copyProperties(upadateCommentByUserParam,comment);

        Date date = new Date();
        comment.setRawUpdateTime(date);

        this.updateById(comment);

        TServiceComment byId = this.getById(comment.getCommentId());

        CommentQueryDto commentQueryDto = new CommentQueryDto();
        BeanUtils.copyProperties(byId,commentQueryDto);

        return commentQueryDto;
    }

    /**
    *@Author 李磊
    *@Description 商家回复
    *@Param [updateCommentByShopParam]
    *@return com.woniu.car.dto.CommentQueryDto
    **/
    @Override
    @Transactional
    public CommentQueryDto updateByShop(UpdateCommentByShopParam updateCommentByShopParam) throws ServiceSException {

        if (null == this.getById(updateCommentByShopParam.getCommentId())) {
            throw new ParamException(CommonConstant.NULLCOMMENTWRONG);
        }

        if (CommonConstant.ONE.equals(this.getById(updateCommentByShopParam.getCommentId()).getStatus())) {
            throw new ParamException(CommonConstant.NULLCOMMENTWRONG);
        }

        if (null != this.getById(updateCommentByShopParam.getCommentId()).getReplyInfo()) {
            throw new ParamException(COMMENTUPDATENULL);
        }

        Date date = new Date();
        TServiceComment comment = new TServiceComment();
        BeanUtils.copyProperties(updateCommentByShopParam,comment);
        comment.setReplyTime(date);

        boolean b = this.updateById(comment);

        if (!b) {
            throw new ServiceSException(CommonConstant.UPDATEWRONG);
        }

        CommentQueryDto commentQueryDto = new CommentQueryDto();
        TServiceComment byId = this.getById(comment.getCommentId());
        BeanUtils.copyProperties(byId,commentQueryDto);

        return commentQueryDto;

    }


    /**
    *@Author 李磊
    *@Description 商家追评回复
    *@Param [updateCommentByShopTParam]
    *@return com.woniu.car.dto.CommentQueryDto
    **/
    @Override
    @Transactional
    public CommentQueryDto updateByShopT(UpdateCommentByShopTParam updateCommentByShopTParam) throws ServiceSException {

        if (null == this.getById(updateCommentByShopTParam.getCommentId())) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        if (CommonConstant.ONE.equals(this.getById(updateCommentByShopTParam.getCommentId()).getStatus())) {
            throw new ParamException(CommonConstant.NULLCOMMENTWRONG);
        }

        if (null == this.getById(updateCommentByShopTParam.getCommentId()).getAdddComment()) {
            throw new ParamException(CommonConstant.AFTERCOMMENTWRONG);
        }

        if (null != this.getById(updateCommentByShopTParam.getCommentId()).getAddedComment()) {
            throw new ParamException(COMMENTUPDATENULL);
        }

        TServiceComment comment = new TServiceComment();
        BeanUtils.copyProperties(updateCommentByShopTParam,comment);

        boolean b = this.updateById(comment);

        if (!b) {
            throw new ServiceSException(CommonConstant.UPDATEWRONG);
        }

        CommentQueryDto commentQueryDto = new CommentQueryDto();
        TServiceComment byId = this.getById(comment.getCommentId());
        BeanUtils.copyProperties(byId,commentQueryDto);

        return commentQueryDto;
    }

    /**
    *@Author 李磊
    *@Description 删除评论
    *@Param [updateCommentParam]
    *@return com.woniu.car.dto.CommentQueryDto
    **/
    @Override
    @Transactional
    public CommentQueryDto updateComment(UpdateCommentParam updateCommentParam) throws ServiceSException {

        Integer userId = updateCommentParam.getUserId();
        Integer commentId = updateCommentParam.getCommentId();
        TServiceComment byId = this.getById(commentId);

        if (null == byId) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }else {
            if (userId != byId.getUserId()) {
                throw new ParamException(CommonConstant.COMMENTDELETE);
            }
        }

        TServiceComment comment = new TServiceComment();
        BeanUtils.copyProperties(updateCommentParam,comment);
        comment.setStatus(CommonConstant.ONE);
        boolean b = this.updateById(comment);

        if (!b) {
            throw new ServiceSException(CommonConstant.UPDATEWRONG);
        }

        CommentQueryDto commentQueryDto = new CommentQueryDto();
        BeanUtils.copyProperties(byId,commentQueryDto);

        return commentQueryDto;
    }


}
